package com.efesco.service.compensation;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.data.ViewBaseField;
import com.bstek.dorado.view.data.ViewField;
import com.bstek.dorado.common.*;
import com.efesco.common.CommUtil;
import com.efesco.system.User;

/**
 * M_apply_form_dataset_add_out_insureListener
 */
public class M_apply_form_dataset_add_out_insureListener extends AbstractDatasetListener {
  public void onInit(Dataset dataset)
     throws Exception {

  }
	DoradoContext context = DoradoContext.getContext();
	User user = (User) context.getAttribute(context.SESSION, "User");
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	  SqlDataset ds1=new SqlDataset();
	  String sql = "SELECT distinct (it.insure_type_id) as product_id,it.product_name"
				+ " FROM s_insurance_employee ie,"
				+ "s_insure_type it,s_employee_on_off_post eoop,"
				+ "s_employee e,s_single_company sc"
				+ " WHERE "
				+ "ie.EMPLOYEE_ID = e.EMPLOYEE_ID AND "
				+ "e.EMPLOYEE_ID = eoop.EMPLOYEE_ID AND sc.SINGLE_COMPANY_ID=eoop.SINGLE_COMPANY_ID and "
				+ "it.insure_type_id=ie.insure_type_id and ie.single_company_id=sc.single_company_id and "
				+ "it.foreign_enterprise_id='"
				+ user.getForeignEnterpriseId()
				+ "' and (ie.on_date<ie.off_date  or ie.off_date is null)"
				+ " and it.is_valid=1";
	 sql+= CommUtil.setConditionSQL(dataset);
	  ds1.setSql(sql);
	  ds1.setPageSize(0);
	
	  ds1.load(true);
	  ds1.moveFirst();

	  while(!ds1.isLast()){
		  String t=ds1.getString("product_id");
		   ViewBaseField field = (ViewBaseField)dataset.addField(t);
		  //ViewField  field = (ViewField)dataset.addField(t);
		   field.setLabel(ds1.getString("product_name"));
		     field.setDataType("double");
		     field.setDefaultValue("0");
		     ds1.moveNext();
	  }
	 
    return true;
  }

  public void afterLoadData(Dataset dataset)
     throws Exception {
String ieId="";
SqlDataset ds1=new SqlDataset();
		String sql = "SELECT ie.insurance_employee_id,ie.employee_id,ie.APPLY_ON_DATE,e.EMPLOYEE_NAME,"
				+ "sc.COMPANY_CHINESE_NAME,it.insure_type_id as product_id,"
				+ "it.product_name,ecfi.money as price "
				+ "FROM s_insurance_employee ie,s_employee_contract_fee_item ecfi,s_employee_contract_fee ecf,"
				+ "s_product p,s_insure_type it,s_employee_on_off_post eoop,"
				+ "s_employee e,s_single_company sc "
				+ "WHERE "
				+ "ie.EMPLOYEE_ID = e.EMPLOYEE_ID AND "
				+ "e.EMPLOYEE_ID = eoop.EMPLOYEE_ID AND sc.SINGLE_COMPANY_ID=eoop.SINGLE_COMPANY_ID and "
				+ "it.insure_type_id=ie.insure_type_id and ecfi.effected=1 and"
				+ " ecf.employee_contract_fee_id=ecfi.employee_contract_fee_id and "
				+ " ecf.employee_on_off_id=eoop.employee_on_off_id and ie.single_company_id=sc.single_company_id and "
				+ "it.product_id=p.product_id and it.insure_type_id=ie.insure_type_id and "
				+ "it.product_id=ecfi.product_id and p.product_id=ecfi.product_id and  it.foreign_enterprise_id='"
				+ user.getForeignEnterpriseId()
				+ "' and (ie.on_date<ie.off_date or ie.off_date is null) ";
sql+= CommUtil.setConditionSQL(dataset);
sql +=" order by ie.employee_id";
ds1.setSql(sql);
ds1.setPageSize(0);
ds1.load(true);
ds1.moveFirst();
int i=0;
while (!ds1.isLast()){
	//System.out.println(i++);
	  if (ieId.equals(ds1.getString("employee_id"))) {
	        dataset.setString(ds1.getString("product_id"),ds1.getString("price"));
	      }
	      else {
	        dataset.insertRecord();
	        ieId=ds1.getString("employee_id");
	        dataset.setString("COMPANY_CHINESE_NAME",ds1.getString("COMPANY_CHINESE_NAME"));
	        dataset.setString("EMPLOYEE_NAME",ds1.getString("EMPLOYEE_NAME"));
	        dataset.setString("APPLY_ON_DATE",ds1.getString("APPLY_ON_DATE"));
	        dataset.setString(ds1.getString("product_id"),ds1.getString("price"));
	      }
	  ds1.moveNext();
}

  }

  public boolean beforeUpdateData(Dataset dataset)
     throws Exception {

    return true;
  }

  public void afterUpdateData(Dataset dataset)
     throws Exception {

  }

}
